package com.stt.design.mediator;

import com.stt.design.command.DeviceCommand;
import org.springframework.stereotype.Component;
import java.util.*;

/**
 * 设备中介者
 * 中介者模式：协调设备间的联动操作
 */
@Component
public class DeviceMediator {
    private final Map<String, List<DeviceCommand>> sceneCommands = new HashMap<>();
    private final List<DeviceCommand> commandHistory = new ArrayList<>();
    
    /**
     * 注册场景命令
     */
    public void registerSceneCommands(String sceneName, List<DeviceCommand> commands) {
        sceneCommands.put(sceneName, commands);
    }
    
    /**
     * 执行场景
     */
    public void executeScene(String sceneName) {
        List<DeviceCommand> commands = sceneCommands.get(sceneName);
        if (commands != null) {
            commands.forEach(command -> {
                command.execute();
                commandHistory.add(command);
            });
        }
    }
    
    /**
     * 撤销上一个命令
     */
    public void undoLastCommand() {
        if (!commandHistory.isEmpty()) {
            DeviceCommand lastCommand = commandHistory.remove(commandHistory.size() - 1);
            lastCommand.undo();
        }
    }
    
    /**
     * 获取命令历史
     */
    public List<String> getCommandHistory() {
        return commandHistory.stream()
            .map(DeviceCommand::getDescription)
            .toList();
    }
} 